package presenter.code.entity;

import utils.Pair;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum CodeType implements ICoder {
    Mode1("11:29:22输入367，完成时间2.5秒", Pattern.compile("11:(\\d+:\\d+)输入(\\d+)，完成时间(\\d+.?\\d+?)秒")),
    Mode2("11:29:22输入367，完成时间2.5秒", Pattern.compile("11:(\\d+:\\d+)输入(\\d+)，完成时间(\\d+)秒"));

    private final Pattern pattern;

    @Override
    public Pattern pattern() {
        return pattern;
    }

    @Override
    public Pair<String, Boolean> stamp(Matcher matcher) {
        String stamp = String.format("11:%s", matcher.group(1));
        boolean stampOver = inValidStamp(matcher);
        return new Pair<>(stamp, stampOver);
    }

    @Override
    public String code(Matcher matcher) {
        return matcher.group(2);
    }

    @Override
    public Pair<String, Boolean> time(Matcher matcher) {
        String time = matcher.group(3);
        boolean timeOver = inValidTime(matcher);
        return new Pair<>(time, timeOver);
    }

    private boolean inValidStamp(Matcher matcher) {
        String stamp = matcher.group(1);
        try {
            float fixedTime = Float.parseFloat(stamp.replace(":", "."));
            if (fixedTime < 29.20 || fixedTime > 30.00) {//不在11:29:20-11:30:00范围内的,都需要标注一下
                return true;
            }
        } catch (Exception ex) {
            //ignore
        }
        return false;
    }

    private boolean inValidTime(Matcher matcher) {
        return Float.parseFloat(matcher.group(3)) > 5.0;
    }

    CodeType(String demo, Pattern pattern) {
        this.pattern = pattern;
    }
}
